<?php //
include_once "_include/DAO.php";

class Table {
    function __construct($tablename, $message = null) {
        $this -> tablename = $tablename;
        $this -> message = $message;
        $this -> mysqli = DAO::connect();
    }

    function __destruct() {
        if ($this -> mysqli) {
            $this -> mysqli -> close();
        }
    }

    //==CRUD METHODS==
    function insert(array $data) {                
        $fields = $this -> fields_list(array_keys($data));
        $values = $this -> values_list(array_values($data));
        $query = "insert into $this->tablename($fields) values($values)";
        $returnValue = $this -> mysqli -> query($query);
        $this -> message = "Can't insert the record(s)";
        if ($returnValue) {
            $this -> message = $this -> mysqli -> affected_rows . " row(s) affected";
        }
        return $returnValue;
    }

    function select($fields = "*", $filter = "", $pagesize = 0, $page = 1) {

        $limit = " ";
        if ($page > 0 && $pagesize > 0) {
            $from = ($page - 1) * $pagesize;
            $limit = "limit $from,$pagesize";
        }

        $query = "select $fields from $this->tablename $filter $limit";        
        $rows = array();
        $this -> message = "Can't select the record(s)";
        if ($result = $this -> mysqli -> query($query)) {
            $rows = $result -> fetch_all(MYSQL_ASSOC);
            $result -> close();
            $this -> message = $this -> mysqli -> affected_rows . " row(s) affected";
        }

        return $rows;

    }

    function pageCount($filter = "", $pagesize = 1) {
        $count = 0;
        try {
            if ($result = $this -> mysqli -> query("SELECT COUNT(*) rowcount from $this->tablename $filter")) {
                if ($row = $result -> fetch_array(MYSQLI_ASSOC)) {
                    $count = $row["rowcount"];
                }
                $result -> close();
            }
        } catch (exception $e) {
            error_log("Exception caught: " . $e -> getMessage() . "\n", 3, "error.log");
        }
        $pagecount = intval($count / $pagesize);
        if ($count % $pagesize != 0)
            $pagecount++;
        return $pagecount;
    }

    function update(array $data, $filter = 1) {
        $updatelist = $this -> update_list($data);
        $query = "update $this->tablename set $updatelist where $filter";
        $returnValue = $this -> mysqli -> query($query);
        $this -> message = "Can't update the record(s)";
        if ($returnValue) {
            $this -> message = $this -> mysqli -> affected_rows . " row(s) affected";
        }
        return $returnValue;
    }

    function delete($filter = 1) {
        $query = "delete from $this->tablename where $filter";
        $returnValue = $this -> mysqli -> query($query);
        $this -> message = "Can't delete the record(s)";
        if ($returnValue) {
            $this -> message = $this -> mysqli -> affected_rows . " row(s) affected";
        }
        return $returnValue;
    }

    //==STRUCTURAL METHODS==
    //Get fields' names of the table
    function fields() {
        $fields = array();
        if ($result = $this -> mysqli -> query("SHOW COLUMNS FROM $this->tablename")) {
            while ($row = $result -> fetch_array(MYSQLI_ASSOC)) {
                $fields[] = $row["Field"];
            }
            $result -> close();
        }
        return $fields;
    }

    //Get fields' names of the primary key of the table
    function keys() {
        $keys = array();
        if ($result = $this -> mysqli -> query("SHOW COLUMNS FROM $this->tablename")) {
            while ($row = $result -> fetch_array(MYSQLI_ASSOC)) {
                if ($row["Key"] == "PRI") {
                    $keys[] = $row["Field"];
                }
            }
            $result -> close();
        }
        return $keys;
    }

    //==UTILITIES METHODS==
    private function fields_list(array $fields) {
        return "`" . implode("`,`", $fields) . "`";
    }

    private function values_list(array $values) {
        return "'" . implode("','", $values) . "'";
    }

    private function update_list(array $data) {
        $list = array();
        foreach ($data as $key => $val) {
            $list[] = "$key='$val'";
        }
        return implode(",", $list);
    }

}

//
?>